package org.apache.lucene.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;


public class Search {
	
	
	public Search() throws CorruptIndexException, LockObtainFailedException, IOException {
		// TODO Auto-generated constructor stub
	
	}
	public static void main(String[] args) throws CorruptIndexException, LockObtainFailedException, IOException {
		
		//final File INDEX_DIR = new File("index");
		final File INDEX_DIR = new File("/Volumes/DATA/index");
		
		final File docDir = new File("/Users/ale/Downloads/wsj"); 				// Change this!!!!!!
		IndexWriter writer= new IndexWriter(INDEX_DIR,new StandardAnalyzer(),true);
		 indexDocs(writer, docDir);
	      System.out.println("Optimizing...");
	      writer.optimize();
	      writer.close();
	      System.out.println("Done");
	}
	static void indexDocs(IndexWriter writer, File file)
    throws IOException {
    // do not try to index files that cannot be read
    if (file.canRead()) {
      if (file.isDirectory()) {
        String[] files = file.list();
        // an IO error could occur
        if (files != null) {
          for (int i = 0; i < files.length; i++) {
            indexDocs(writer, new File(file,files[i]));
          }
        }
      } else {
        System.out.println("adding " + file);
        try {
          
         Document[] Docs = FileDocument.Documents(file);
         for(Document D : Docs)
        	 writer.addDocument(D);
        
        	//writer.addDocument(FileDocument.Document(file));
        	}
        // at least on windows, some temporary files raise this exception with an "access denied" message
        // checking if the file can be read doesn't help
        catch (FileNotFoundException fnfe) {
          ;
        }
      }}}
    
}
